1 /*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import sun.net.util.*;
25
26 /*
27 * Unit test for sun.net.util.IPAddressUtil
28 */
29 public class IPAddressUtilTest {
30 final static Boolean good = Boolean.TRUE;
31 final static Boolean bad = Boolean.FALSE;
32
33 static Object [][] assertions = {
34 {"224.0.1.0", good},
35 {"238.255.255.255", good},
36 {"239.255.255.255", good},
37 {"238.255.255.2550", bad},
38 {"256.255.255.255", bad},
39 {"238.255.2550.255", bad},
40 {"238.2550.255.255", bad},
41 {"2380.255.255.255", bad},
42 {"10::10", good},
43 {"10::10.1", bad},
44 {"10::10.1.2", bad},
45 {"10::10.1.2.3", good},
46 {"10::10.1.2.3.4", bad},
47 {"10::10.", bad},
48 {"10::.10.", bad},
49 {"10::.10", bad}
50 };
51
52 public static void main(String[] args) {
53 for (int i=0; i<assertions.length; i++) {
54 String addr = (String) assertions[i][0];
55 boolean expected = ((Boolean) assertions[i][1]).booleanValue();
56
57 boolean result = true;
58
59 if (!IPAddressUtil.isIPv4LiteralAddress(addr) &&
60 !IPAddressUtil.isIPv6LiteralAddress(addr)) {
61 result = false;
62 }
63
64 if (result != expected) {
65 throw new RuntimeException ("wrong result for " + addr);
66 }
67 }
68 }
69 }